home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_65 / play_rec.cpp < prev    next >
C/C++ Source or Header  |  1995-01-01  |  2KB  |  86 lines

  1. // Play_rec.cpp
  2.  
  3. // Plays or records sound files with the Sound Blaster.
  4. // This is a test program to demonstrate using the sound class.
  5.  
  6. // Written by Christopher M. Box (1993)
  7. // Memory allocation code was drawn from the SBF DACDMA program
  8.  
  9. #define DMA_BUF_SIZE 32000U
  10. #define ALLOCATE ((2*DMA_BUF_SIZE) + 65536L)
  11.  
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <alloc.h>
  17. #include <dos.h>
  18. #include <io.h>
  19.  
  20. #include "sndclass.h"
  21.  
  22. #define Dirstring (dir==PLAY ? "Play" : "Record")
  23.  
  24. void inc_arg(int &argc, char **&argv) {
  25.     ++argv;
  26.     if (--argc < 1) {
  27.         cprintf("play_rec [-r] [-s sample rate] sample_file\r\n");
  28.         cprintf("-r: Record instead of play\r\n");
  29.         exit(1);
  30.     }
  31. }
  32.  
  33. int main(int argc, char **argv) {
  34.     unsigned sr=10000;         // Default sample rate
  35.     int dir = PLAY;
  36.  
  37.     inc_arg(argc, argv);
  38.  
  39.     if (! strcmp(*argv, "-r")) {
  40.         dir = RECORD;
  41.         inc_arg(argc, argv);
  42.     }
  43.     if (! strcmp(*argv, "-s")) {
  44.         inc_arg(argc, argv);
  45.         sr = atoi(*argv);        // Works for unsigned ints too
  46.         inc_arg(argc, argv);
  47.     }
  48.  
  49.     SoundDevice *sdev;
  50.     sdev = new SbDevice;         // This could also be created statically
  51.     if (! sdev -> install_ok()) exit(1);
  52.  
  53. // Open/create the file.
  54.     int handle;
  55.     if (dir == RECORD) {
  56.         handle = _creat(*argv, 0);
  57.     } else {
  58.         handle = _open(*argv, 1);
  59.     }
  60.     if (handle < 0) {
  61.         cprintf("Could not open sample file '%s'\r\n",*argv);
  62.         exit(1);
  63.     }
  64.  
  65. // Obtain an aligned 64K memory buffer for the DMA functions
  66.     byte far *raw = (byte far *) farmalloc(ALLOCATE);
  67.     if (! raw) {
  68.         cprintf("Not enough memory available - an extra %uK needed.\r\n",
  69.             ((unsigned int)(ALLOCATE-farcoreleft()))/1024+1);
  70.         exit(1);
  71.     }
  72.     long physical = ((long)FP_OFF(raw)) + (((long)FP_SEG(raw)) << 4);
  73.     long aligned_physical = (physical+0x0FFFFL) & 0xF0000L;
  74.     byte far *buf = (byte far *)
  75.         MK_FP((unsigned )((aligned_physical >> 4) & 0xFFFF),0);
  76.  
  77. // Now simply call the Sound Device functions to record/play the file.
  78.     sdev -> set_rate(sr,dir);
  79.     cprintf("%sing sample at %uHz\r\n",Dirstring, sdev -> get_rate());
  80.     cprintf("Press 'p' to pause.\r\n");
  81.     sdev -> file_dma(handle,buf,DMA_BUF_SIZE,0,dir);
  82.     _close(handle);
  83.     farfree(raw);
  84.     cprintf("Done.\r\n");
  85.     return 0;
  86. }